
package inf3791.calendarsystem.others.panels.calendar;

import inf3791.calendarsystem.others.components.editing.AppointmentEditForm;
import inf3791.calendarsystem.shared.Appointment;
import java.awt.Component;
import java.util.EventObject;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;

/**
 *
 * @author Jonas Weber
 */
class AppointmentCellEditor extends javax.swing.JPanel implements javax.swing.table.TableCellEditor {

    AppointmentEditForm aef;

    /**
     * Creates new form AppointmentCellEditor
     */
    public AppointmentCellEditor() {
        initComponents();

    }

    @Override
    public Component getTableCellEditorComponent(JTable jtable, final Object o, boolean bln, int i, int i1) {
        appointmentWidget1.setAppointment((Appointment) o);


        aef = new AppointmentEditForm() {
            @Override
            protected Appointment getInitialAppointment() {
                return (Appointment) o;
            }

            @Override
            protected String getButton1Text() {
                return "Save";
            }

            @Override
            protected String getButton2Text() {
                return "Discard";
            }

            @Override
            protected String getWindowTitle() {
                return ((Appointment) o).getTitle();
            }

            @Override
            protected void onButton1(Appointment x) {
                appointmentWidget1.setAppointment(x);
                setVisible(false);
                fireEditingStopped();
            }

            @Override
            protected void onButton2(Appointment x) {
                setVisible(false);
                fireEditingCanceled();
            }
        };
        return this;
    }

    @Override
    public Object getCellEditorValue() {
        return appointmentWidget1.getAppointment();
    }

    @Override
    public boolean isCellEditable(EventObject eo) {
        return true;
    }

    @Override
    public boolean shouldSelectCell(EventObject eo) {
        return true;
    }

    @Override
    public boolean stopCellEditing() {
        return true;
    }

    @Override
    public void cancelCellEditing() {
        aef.setVisible(false);
    }
    private List<CellEditorListener> listeners = new LinkedList<>();

    @Override
    public void addCellEditorListener(CellEditorListener cl) {
        listeners.add(cl);
    }

    @Override
    public void removeCellEditorListener(CellEditorListener cl) {
        listeners.remove(cl);
    }

    private void fireEditingStopped() {
        for (CellEditorListener cl : listeners) {
            cl.editingStopped(new ChangeEvent(this));
        }
    }

    private void fireEditingCanceled() {
        for (CellEditorListener cl : listeners) {
            cl.editingCanceled(new ChangeEvent(this));
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        appointmentWidget1 = new inf3791.calendarsystem.others.panels.calendar.AppointmentWidget();

        setBackground(new java.awt.Color(112, 112, 112));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });

        jButton1.setText("Edit");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        appointmentWidget1.setBackground(new java.awt.Color(112, 112, 112));
        appointmentWidget1.setMaximumSize(new java.awt.Dimension(314, 50));
        appointmentWidget1.setMinimumSize(new java.awt.Dimension(314, 50));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(appointmentWidget1, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addComponent(jButton1))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(appointmentWidget1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (aef != null) {
            aef.setVisible(true);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        if (evt.getClickCount() == 2) {
            if (aef != null) {
                aef.setVisible(true);
            }
        }
    }//GEN-LAST:event_formMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private inf3791.calendarsystem.others.panels.calendar.AppointmentWidget appointmentWidget1;
    private javax.swing.JButton jButton1;
    // End of variables declaration//GEN-END:variables
}
